/*
 AMENDMENTS:
 DATE          NAME        VERSION     DESCRIPTION
--------       -----       -------     -----------
 16/04/2013    MKANG       	1.0.0       1.LogInAction.java : Add to avoid refresh / back action from browser.
*/
package com.mbbmap.security.action;

import java.util.Properties;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mbbmap.app.home.ParamHome;
import com.mbbmap.app.manager.ConfigManager;
import com.mbbmap.util.Constants;


/**
 *
 */
public final class LogInAction extends Action {
	static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMddHHmmss");


  /**
   * Login Action
   */
  public ActionForward execute(ActionMapping mapping,
    ActionForm form, HttpServletRequest request,
    HttpServletResponse response) throws Exception {

	HttpSession session = request.getSession(false);
	boolean DEBUG = ((Boolean)this.getServlet().getServletContext().getAttribute(Constants.DEBUG)).booleanValue();
	//System.out.println("LoginAction.java DEBUG="+DEBUG);
	session.setAttribute(Constants.MERCHANT_KEY, Constants.APP_MERCHANT);
	session.setAttribute(Constants.PROPERTIES_PARAMETER, Constants.globalParamProp);
	String nextPage = "";

	if (ConfigManager.getInstance().get(Constants.PRODUCTION).equals(Constants.DB_NO)) {
		session.setAttribute( Constants.LOGON_ERR_MSG, ConfigManager.getInstance().get(Constants.MSG_LOGON_PRODUCTION_OFF));
		return mapping.findForward(Constants.KEY_ACTION_ERR_LOGON);
	}
	
	Properties paramProp = Constants.globalParamProp;
	String strDisFr = paramProp.getProperty(ParamHome.SEC_DISABLE_TIMESTAMP_FR);
	String strDisTo = paramProp.getProperty(ParamHome.SEC_DISABLE_TIMESTAMP_TO);
	
	if(strDisFr==null || strDisFr.equals("")){
		strDisFr="0";
	}
	if(strDisTo==null || strDisTo.equals("")){
		strDisTo="0";
	}
	
	Double dRestrictDateFr = Double.parseDouble(strDisFr);
	Double dRestrictDateTo = Double.parseDouble(strDisTo);
	long lDisFr = Long.parseLong(strDisFr);
	long lDisTo = Long.parseLong(strDisTo);
	//201403251302101
	System.out.println("...LogInAction dRestrictDateFr=" + dRestrictDateFr.toString());
	System.out.println("... LogInAction dRestrictDateTo=" + dRestrictDateTo.toString());
	System.out.println("...LogInAction lDisFr=" + lDisFr);
	System.out.println("... LogInAction lDisTo=" + lDisTo);

	Calendar cal = Calendar.getInstance();
	
	String curDate = dateFormatter.format(cal.getTime()).toString();
	Double dCurrTimestamp = Double.parseDouble(curDate);
	long lCurDate = Long.parseLong(curDate);
	System.out.println("... LogInAction lCurDate=" + lCurDate);
	boolean bRestricted=false;

	if((lCurDate > lDisFr) && (lCurDate < lDisTo) ){
		bRestricted=true;
	}
		System.out.println("... user id is blank bRestricted=" + bRestricted);

	int maxWebConnAllowed = Integer.parseInt((paramProp.getProperty(ParamHome.SEC_MAX_WEB_CON)).trim());
	//int maxWebConnAllowed = 1;
	//if (DEBUG) System.out.println("... user id is blank maxWebConnAllowed=" + maxWebConnAllowed);
	System.out.println("... user id is blank maxWebConnAllowed=" + maxWebConnAllowed);
	
	int curTotalWebUserConn = 1;
	String strTmp = (String) session.getAttribute("eremitsessioncount");
	System.out.println("... strTmp=" + strTmp);
	if (strTmp!=null){
	curTotalWebUserConn = Integer.parseInt(strTmp);}
	/*
	if (curTotalWebUserConn >= maxWebConnAllowed){
		String strErrLogonMsg = ConfigManager.getInstance().get(Constants.MSG_MAX_WEB_USER_CONN);
		session.setAttribute(Constants.LOGON_ERR_MSG, strErrLogonMsg);
		nextPage = Constants.KEY_ACTION_ERR_LOGON;
	}else{
		nextPage = Constants.KEY_ACTION_SUCCESS;
	}
	*/
	if(bRestricted){
		String strErrLogonMsg = ConfigManager.getInstance().get(Constants.MSG_LOGON_ERR_NO_ACCESS_DATE);
		session.setAttribute(Constants.LOGON_ERR_MSG, strErrLogonMsg);
		nextPage = "error";
	}else{
		nextPage = Constants.KEY_ACTION_SUCCESS;
	}

	//eremitsessioncount
	//if (DEBUG) System.out.println("LogInAction.execute: END");

    return mapping.findForward(nextPage);
  }

}
